//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LastFixingQuote.h"
using namespace Cephei::QL::Quotes;
#include <gen/QL/Index.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (Cephei::QL::IIndex^ index) : CQuote(CLastFixingQuote::typeid)
{
    CIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phLastFixingQuote = NULL;
#endif
        _Cindex = safe_cast<CIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::Index>& _index = static_cast<boost::shared_ptr<QuantLib::Index>&> (_Cindex->GetShared ()); 
        _ppLastFixingQuote = new boost::shared_ptr<QuantLib::LastFixingQuote> (new QuantLib::LastFixingQuote ( _index ));
        SetQuote (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppLastFixingQuote));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (boost::shared_ptr<QuantLib::LastFixingQuote>& childNative, Object^ owner) : CQuote(CLastFixingQuote::typeid)
{
#ifdef HANDLE
	_phLastFixingQuote = NULL;
#endif
	_ppLastFixingQuote = &childNative;
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppLastFixingQuote));
}
Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (QuantLib::LastFixingQuote& childNative, Object^ owner) : CQuote(CLastFixingQuote::typeid)
{
#ifdef HANDLE
	_phLastFixingQuote = NULL;
#endif
	_ppLastFixingQuote = new boost::shared_ptr<QuantLib::LastFixingQuote> (&childNative);
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppLastFixingQuote));
    _LastFixingQuoteOwner = owner;
    _QuoteOwner = owner;
}

Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (CLastFixingQuote^ copy) : CQuote(CLastFixingQuote::typeid)
{
#ifdef HANDLE
	_phLastFixingQuote = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLastFixingQuote = new boost::shared_ptr<QuantLib::LastFixingQuote> (copy->GetShared());
        _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppLastFixingQuote));
    }
}
Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (System::Type^ t) : CQuote(CLastFixingQuote::typeid)
{
#ifdef HANDLE
	_phLastFixingQuote = NULL;
#endif
	if (!t->IsSubclassOf(CLastFixingQuote::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (QuantLib::Handle<QuantLib::LastFixingQuote>& childNative, Object^ owner)  : CQuote(CLastFixingQuote::typeid)
{
	_phLastFixingQuote = &childNative;
	_ppLastFixingQuote = &static_cast<boost::shared_ptr<QuantLib::LastFixingQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppLastFixingQuote));
    _LastFixingQuoteOwner = owner;
}
Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (QuantLib::Handle<QuantLib::LastFixingQuote> childNative)  : CQuote(CLastFixingQuote::typeid)
{
	_phLastFixingQuote = &childNative;
	_ppLastFixingQuote = &static_cast<boost::shared_ptr<QuantLib::LastFixingQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppLastFixingQuote));
}
#endif
#ifdef STRUCT
Cephei::QL::Quotes::CLastFixingQuote::CLastFixingQuote (QuantLib::LastFixingQuote childNative)  : CQuote(CLastFixingQuote::typeid)
{
#ifdef HANDLE
	_phLastFixingQuote = NULL;
#endif
	_ppLastFixingQuote = new boost::shared_ptr<QuantLib::LastFixingQuote> (new QuantLib::LastFixingQuote (childNative));
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppLastFixingQuote));
}
#endif

Cephei::QL::Quotes::CLastFixingQuote::~CLastFixingQuote ()
{
    if (_ppLastFixingQuote != NULL)
    {
	    delete _ppLastFixingQuote;
        _ppLastFixingQuote = NULL;
    }
}
Cephei::QL::Quotes::CLastFixingQuote::!CLastFixingQuote ()
{
    if (_ppLastFixingQuote != NULL)
    {
	    delete _ppLastFixingQuote;
    }
}
QuantLib::LastFixingQuote& Cephei::QL::Quotes::CLastFixingQuote::GetReference ()
{
    if (_ppLastFixingQuote == NULL) throw gcnew NativeNullException ();
	return **_ppLastFixingQuote;
}
boost::shared_ptr<QuantLib::LastFixingQuote>& Cephei::QL::Quotes::CLastFixingQuote::GetShared ()
{
    if (_ppLastFixingQuote == NULL) throw gcnew NativeNullException ();
	return *_ppLastFixingQuote;
}
QuantLib::LastFixingQuote* Cephei::QL::Quotes::CLastFixingQuote::GetPointer ()
{
    if (_ppLastFixingQuote == NULL) throw gcnew NativeNullException ();
	return &**_ppLastFixingQuote;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LastFixingQuote>& Cephei::QL::Quotes::CLastFixingQuote::GetHandle ()
{
	if (_phLastFixingQuote == NULL)
	{
		_phLastFixingQuote = new Handle<QuantLib::LastFixingQuote> (*_ppLastFixingQuote);
	}
	return *_phLastFixingQuote;
}
#endif
bool Cephei::QL::Quotes::CLastFixingQuote::HasNative () 
{
	return (_ppLastFixingQuote != NULL);
}

Cephei::QL::IIndex^ Cephei::QL::Quotes::CLastFixingQuote::Index::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Index>& _rv = (boost::shared_ptr<QuantLib::Index>&)(*_ppLastFixingQuote)->index ( );   
        Cephei::QL::CIndex^ _nrv = gcnew Cephei::QL::CIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Quotes::CLastFixingQuote::IsValid::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppLastFixingQuote)->isValid ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Quotes::CLastFixingQuote::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppLastFixingQuote)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Quotes::ILastFixingQuote^ Cephei::QL::Quotes::CLastFixingQuote::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppLastFixingQuote)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Quotes::CLastFixingQuote::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLastFixingQuote)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Quotes::ILastFixingQuote^ Cephei::QL::Quotes::CLastFixingQuote_Factory::Create (Cephei::QL::IIndex^ index)
{
    return gcnew CLastFixingQuote ( index);
}
